package com.douban.book.service;

import java.io.File;
import java.io.IOException;
import java.util.Set;
import java.util.TreeSet;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.douban.book.bean.Book;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

/**
 * Created by Administrator on 2018/2/28.
 */

public class Spider {

	
	public  Set<Book> getResources(String url,String topic) {
		TreeSet<Book> set = new TreeSet<Book>();
		for (Book book : set) {
			System.out.println(book);
		}
		Document doc = null;
		try {
			System.out.println("xxxxxxxxxxxxxx-:   "+url);
			doc = Jsoup.connect(url).get();
			Elements elements = doc.select("a.tag");
			for (Element element : elements) {
				if (topic.equals(element.text())) {
					String href = element.attr("href");
					String destination = url + href;
					Document document = Jsoup.connect(destination).get();
					//打印整个网页内容
//					System.out.println(document.outerHtml());
					Elements books = document.select("div.info");
					for (Element book : books) {
						String text = book.select("span.pl").text();
						int personNum = Integer.parseInt(text.replaceAll("[^\\d]+", ""));
						if (personNum > 1000) {
							Element child = book.child(0);
							String bookName = child.child(0).text();
							float score = Float.parseFloat(book.select("span.rating_nums").text());
							String template = book.select("div.pub").text();
							String[] split = template.split("/");
							String author = split[0];
							String publish = null;
							String publishDate = null;
							String price = null;
							if(split.length<5){
								publish = split[1];
								publishDate = split[2];
								price = split[3];
							}else{
								publish = split[2];
								publishDate = split[3];
								price = split[4];
							}
							Book book2 = new Book(bookName, score, personNum, author, publish, publishDate, price);
							set.add(book2);
						}
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return set;
	}
}
